package com.example.muasic.fragment;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.Bundle;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.TextView;

import com.example.muasic.R;
import com.example.muasic.activity.Favourite;
import com.example.muasic.activity.MainActivity;
import com.example.muasic.activity.PlayMusicActivity;
import com.example.muasic.activity.PlayingActivity;
import com.example.muasic.activity.RecMusicActivity;
import com.example.muasic.adapter.myAdapter;
import com.example.muasic.info.Info;
import com.example.muasic.info.item;
import com.example.muasic.provider.MusicSqLiteHelper;
import com.example.muasic.service.IService;
import com.example.muasic.service.MusicService;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import static android.content.Context.BIND_AUTO_CREATE;

public class MyFragment extends Fragment {

    private myAdapter adapter;
    ListView listView;
    TextView textView;
    View view;
    private static IService iService;
    private ServiceConnection conn;
    private MusicSqLiteHelper musicSqLiteHelper;
    private static List<String> titleList = new ArrayList<>();
    private static List<Info> musicList = new ArrayList<>();
    private String TAG = "PlayMusicActivity";
    private static List<item> mList = new ArrayList<>();
    public static MyFragment newInstance() {
        MyFragment fragment = new MyFragment();
        return fragment;
    }

    public MyFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.fragment_my, container, false);
        musicSqLiteHelper = new MusicSqLiteHelper(getActivity(),"music.db",1);


        // 开启服务
        Intent intent = new Intent(getActivity(), MusicService.class);
        this.getActivity().startService(intent);
        conn = new ServiceConnection(){
            @Override
            public void onServiceConnected(ComponentName name, IBinder service) {
                //6. 获取IBinder对象, 以此调用暴露的函数
                iService = (IService) service;
            }

            @Override
            public void onServiceDisconnected(ComponentName name) {
            }
        };

        //绑定服务

        this.getActivity().bindService(intent, conn, BIND_AUTO_CREATE);


        initView();
        initData();
        initAdapter();
        titleList=((MainActivity)getActivity()).gettitleList();
        musicList=((MainActivity)getActivity()).getmusicList();


        textView = (TextView) view.findViewById(R.id.myfav);
        textView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent1=new Intent(getActivity(), Favourite.class);
                startActivity(intent1);
            }
        });
        return view;

    }

    private void initView() {
        listView = (ListView) view.findViewById(R.id.list);
    }

    //添加bean类数据临时的先添加，再连数据库
    private void initData() {
        mList = new ArrayList<>();
        for (int i = 0; i < 5; i++) {
            item item1 = new item();
            item1.setPic(i);
            item1.setText("本地");
            mList.add(item1);
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
                        if(i==0) {
                            Intent intent = new Intent(getActivity(), PlayMusicActivity.class);
                            Bundle bundle = new Bundle();
                            bundle.putSerializable("title_send", (Serializable) titleList);
                            bundle.putSerializable("musiclist1", (Serializable) musicList);
                            intent.putExtras(bundle);
                            startActivity(intent);
                        }else if (i==1){
                            Intent intent = new Intent(getActivity(), RecMusicActivity.class);
                            startActivity(intent);
                        }

                    }
                });

        }
    }

    private void initAdapter() {
        adapter = new myAdapter(getActivity(), mList);
        listView.setAdapter(adapter);

    }

}
